<template>
  <BasicLayout>
    <template #wrapper>
      <el-card class="box-card">
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
          <el-form-item label="用户名称" prop="username">
            <el-input
              v-model="queryParams.username"
              placeholder="请输入用户名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <el-input
              v-model="queryParams.phone"
              placeholder="请输入手机号码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-permisaction="['admin:tuser:remove']"
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除
            </el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="tuserList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50px" align="center"/>
          <el-table-column label="ID" width="50px" align="center" prop="userId"/>
          <el-table-column label="名下链接" width="100px" align="center" prop="urlsLink" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <router-link :to="{name:'UrlList', params: {userId:scope.row.userId}}" class="link-type">
                <span>点击跳转</span>
              </router-link>
            </template>
          </el-table-column>
          <el-table-column label="累计充值" width="100px" align="center" prop="rechargeLink" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <router-link :to="{name:'OrderList', params: {type:1, userId:scope.row.userId}}" class="link-type">
                <span>点击跳转</span>
              </router-link>
            </template>
          </el-table-column>
          <el-table-column label="用户名称" width="200px" align="center" prop="username" :show-overflow-tooltip="true"/>
          <el-table-column label="用户昵称" width="150px" align="center" prop="nickName" :show-overflow-tooltip="true"/>
          <el-table-column label="手机号码" width="130px" align="center" prop="phone"/>
          <el-table-column label="用户星级" width="100px" align="center" prop="starLevel"/>
          <el-table-column label="用户标签" width="100px" align="center" prop="tag"/>
          <el-table-column label="信用分" width="60px" align="center" prop="creditScore"/>
          <el-table-column label="创建时间" width="180px" align="center" prop="createdAt">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createdAt) }}</span>
            </template>
          </el-table-column>
<!--          <el-table-column label="注册IP" width="100px" align="center" prop="ip"/>-->
<!--          <el-table-column label="最后登陆IP" width="100px" align="center" prop="lastLoginIp"/>-->
          <el-table-column width="300px" label="标注和操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                v-permisaction="['admin:tuser:edit']"
                size="mini"
                type="text"
                @click="handleUpdateBs(scope.row)"
              >调倍数
              </el-button>
<!--              <el-button-->
<!--                v-permisaction="['admin:tuser:edit']"-->
<!--                size="mini"-->
<!--                type="text"-->
<!--                @click="handleUpdateXy(scope.row)"-->
<!--              >调信用分-->
<!--              </el-button>-->
              <el-button
                v-permisaction="['admin:tuser:edit']"
                size="mini"
                type="text"
                @click="handleUpdateSt(scope.row)"
              >打标签
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleUpdateYue(scope.row)"
              >余额充值
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-card>

      <el-dialog :title="title" :visible.sync="openBs" width="500px">
        <el-scrollbar>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="用户倍数" prop="starLevel">
              <el-input-number controls-position="right" v-model="form.beishu" :min="1" :max="1000"
                               label="调整倍数"></el-input-number>
            </el-form-item>
          </el-form>
        </el-scrollbar>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFormBs">确 定</el-button>
          <el-button @click="cancelBs">取 消</el-button>
        </div>
      </el-dialog>
      <el-dialog :title="title" :visible.sync="openYue" width="500px">
        <el-scrollbar>
          <el-form ref="orderForm" :model="orderForm" :rules="rules" label-width="80px">
            <el-form-item label="充值数量" prop="starLevel">
              <el-input-number controls-position="right" v-model="orderForm.rechargeAmount" :min="1" :max="1000"
                               label="增加余额"></el-input-number>
            </el-form-item>
          </el-form>
        </el-scrollbar>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFormYue">确 定</el-button>
          <el-button @click="cancelYue">取 消</el-button>
        </div>
      </el-dialog>

      <el-dialog :title="title" :visible.sync="openXy" width="500px">
        <el-scrollbar>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="用户信用分" label-width="90px" prop="starLevel">
              <el-input-number controls-position="right" v-model="form.creditScore" :min="1" :max="1000"
                               label="调整倍数"></el-input-number>
            </el-form-item>
          </el-form>
        </el-scrollbar>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFormXy">确 定</el-button>
          <el-button @click="cancelXy">取 消</el-button>
        </div>
      </el-dialog>
      <el-dialog :title="title" :visible.sync="openSt" width="500px">
        <el-scrollbar>
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-select v-model="form.tag" placeholder="请选择标签">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <!--            <el-form-item label="用户打标签" prop="starLevel">-->
            <!--              <el-input v-model="form.tag" placeholder="用户标签"/>-->
            <!--            </el-form-item>-->
          </el-form>
        </el-scrollbar>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFormSt">确 定</el-button>
          <el-button @click="cancelSt">取 消</el-button>
        </div>
      </el-dialog>
    </template>
  </BasicLayout>
</template>

<script>
  import {addTUser, delTUser, getAccount, getTUser, listTUser, updateTUser} from '@/api/tuser'
  import {addRechargeOrder} from '@/api/torder'
  import store from "@/store";

  export default {
    name: 'TUser',
    components: {},
    data() {
      return {
        userId: 0,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        openXy: false,
        openBs: false,
        openSt: false,
        openYue: false,
        isEdit: false,
        fileOpen: false,
        fileIndex: undefined,
        // 类型数据字典
        typeOptions: [],
        tuserList: [],

        // 关系表类型

        orderForm: {},
        // 查询参数
        queryParams: {
          pageIndex: 1,
          pageSize: 10,

        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        options: [{
          value: '正经用户',
          label: '正经用户'
        }, {
          value: '灰色用户',
          label: '灰色用户'
        }, {
          value: '黑色用户',
          label: '黑色用户'
        }],
      }
    },
    created() {
      this.getList()
      // this.getUserAccount()
    },
    methods: {
      getUserAccount() {
        getAccount(parseInt(store.getters.userId)).then(response => {
          if (response.code === 200) {
            this.orderForm.userId = parseInt(store.getters.userId)
            // this.orderForm.phone = store.getters.phone
          }
        })
      },
      addOrder() {
        addRechargeOrder(this.orderForm).then(response => {
          // if (response.code == 200) {
          //   this.orderForm.id = response.id
          // }
        })
      },
      /** 查询参数列表 */
      getList() {
        this.loading = true
        listTUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.tuserList = response.data.list
            this.total = response.data.count
            this.loading = false
          }
        )
      },
      // 取消按钮
      cancelBs() {
        this.openBs = false
        this.reset()
      },
      cancelXy() {
        this.openXy = false
        this.reset()
      },
      cancelSt() {
        this.openSt = false
        this.reset()
      },
      cancelYue() {
        this.openYue = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          userId: undefined,
          username: undefined,
          password: undefined,
          nickName: undefined,
          phone: undefined,
          roleId: undefined,
          salt: undefined,
          avatar: undefined,
          sex: undefined,
          email: undefined,
          remark: undefined,
          beishu: undefined,
          status: undefined,
          starLevel: undefined,
          creditScore: undefined,
          location: undefined,
          ip: undefined,
          tag: undefined
        }
        this.resetForm('form')
      },
      getImgList: function () {
        this.form[this.fileIndex] = this.$refs['fileChoose'].resultList[0].fullUrl
      },
      fileClose: function () {
        this.fileOpen = false
      },
      // 关系
      // 文件
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageIndex = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = []
        this.resetForm('queryForm')
        this.handleQuery()
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加用户'
        this.isEdit = false
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.USERID)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      handleUpdateBs(row) {
        this.reset()
        this.userId = row.userId
        getTUser(this.userId).then(response => {
          this.form = response.data
          this.openBs = true
          this.title = '调整倍数'
        })
      },
      handleUpdateXy(row) {
        this.reset()
        this.userId = row.userId
        getTUser(this.userId).then(response => {
          this.form = response.data
          this.openXy = true
          this.title = '修改用户信用分'
        })
      },
      handleUpdateSt(row) {
        this.reset()
        this.userId = row.userId
        getTUser(this.userId).then(response => {
          this.form = response.data
          this.openSt = true
          this.title = '修改用户状态'
        })
      },
      handleUpdateYue(row) {
        this.reset()
        this.openYue = true
        this.orderForm.userId = row.userId
      },
      /** 提交按钮 */
      submitFormBs: function () {
        this.$refs['form'].validate(valid => {
          this.form.userId = this.userId
          updateTUser(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess('修改成功')
              this.openBs = false
              this.getList()
            } else {
              this.msgError(response.msg)
            }
          })
        })
      },
      submitFormXy: function () {
        this.$refs['form'].validate(valid => {
          this.form.userId = this.userId
          updateTUser(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess('修改成功')
              this.openXy = false
              this.getList()
            } else {
              this.msgError(response.msg)
            }
          })
        })
      },
      submitFormYue: function () {
        this.$refs['orderForm'].validate(valid => {
          addRechargeOrder(this.orderForm).then(response => {
            if (response.code === 200) {
              this.msgSuccess('增加成功')
              this.openYue = false
              this.getList()
            } else {
              this.msgError(response.msg)
            }
          })
        })
      },
      submitFormSt: function () {
        this.$refs['form'].validate(valid => {
          this.form.userId = this.userId
          updateTUser(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess('修改成功')
              this.openSt = false
              this.getList()
            } else {
              this.msgError(response.msg)
            }
          })
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        var Ids = (row.USERID && [row.USERID]) || this.ids

        this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delTUser({'ids': Ids})
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(function () {
        })
      }
    }
  }
</script>
